#include "ResultDialog.h"
#include "Config.h"
#include "MainLayer.h"
#include "MainScene.h"
#include "ObjectDataDefine.h"
#include "GlobalData.h"
#include "MathUtil.h"


ResultDialog::ResultDialog(void)
{
	_lblExp = NULL;
	_lblGold = NULL;
}


ResultDialog::~ResultDialog(void)
{
	CC_SAFE_RELEASE(_lblExp);
	CC_SAFE_RELEASE(_lblGold);
}

bool ResultDialog::init()
{
	if (!CCNode::init())
		return false;

	CCSprite* bg = CCSprite::create("result_dialog_img.png");
	bg->setPosition(ccp(RESOLUTION_WIDTH/2, RESOLUTION_HEIGHT/2));
	addChild(bg);

	CCMenu* menu = CCMenu::create();
	menu->setPosition(CCPointZero);
	menu->setAnchorPoint(CCPointZero);
	addChild(menu);

	CCMenuItemImage* butOk = CCMenuItemImage::create("button_png.png", "button_png.png", "button_png.png", this, SEL_MenuHandler(&ResultDialog::callbackOk));
	butOk->setPosition(ccp(380, 540));
	menu->addChild(butOk);

	CCLabelTTF* lblExp = CCLabelTTF::create("0", "Arial", 50, CCSizeMake(100, 60), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter);
	lblExp->setPosition(ccp(500, 645));
	addChild(lblExp);
	setLblExp(lblExp);

	CCLabelTTF* lblGold = CCLabelTTF::create("0", "Arial", 50, CCSizeMake(100, 60), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter);
	lblGold->setPosition(ccp(250, 645));
	addChild(lblGold);
	setLblGold(lblGold);

	return true;
}

void ResultDialog::parseData( MainLayer* mainLayer )
{
	int score = mainLayer->getScore();
	int star = mainLayer->getStar();
	float kilomet = mainLayer->getKilomet();

	_lblExp->setString(MathUtil::IntToString(star).c_str());
	_lblGold->setString(MathUtil::IntToString(kilomet).c_str());

	PlayerData* playerData = GlobalData::sharedData()->getPlayerData();
	playerData->setGold(playerData->getGold() + static_cast<int>(kilomet));
	playerData->setUserExp(playerData->getUserExp() + star);
}

void ResultDialog::callbackOk( CCObject* sender )
{
	// Stuff with global game data

	CCDirector::sharedDirector()->replaceScene(MainScene::scene());
}
